package dq.study.concurrent.threadpool;

import java.util.Random;
import java.util.concurrent.*;

/**
 * @author dongqun
 * @version 1.0
 * @description ${description}
 * @date 2019/9/4 17:06
 */
public class MyThreadPool {




    public static void main(String[] args) throws InterruptedException {
        //任务提交
        ArrayBlockingQueue<Runnable> queue = new ArrayBlockingQueue<>(1000);
        ThreadPoolExecutor pool = new ThreadPoolExecutor(5,1000,4L,TimeUnit.MILLISECONDS,
                queue,
                Executors.defaultThreadFactory(),
                new ThreadPoolExecutor.AbortPolicy());
        for (int i=0;i<1000;i++){
            synchronized (MyThreadPool.class){
                pool.execute(new MyTask());
                System.out.println(i);
            }
            System.out.println("---"+pool.getQueue().size());
        }


        // shutdown与shutdownNow 模拟
        /**
         * @author dongqun
         * @description  shutdown 不在接收新任务，将已经提交的任务执行完再提交
         *                shutdownNow 将正在活跃的任务线程中断，并且返回等待的任务
         * @date 2019/9/5 10:33
         * @param [args]
         * @return void
         */
        class MyThread implements Runnable{
            public void run() {
                try {
                    Random random = new Random();
                    int time = 1000 * (random.nextInt(5)+1);
                    Thread.sleep(time);
                    System.out.println(Thread.currentThread().getName() + " complete , time = "+time);
                } catch (InterruptedException e) {
                    System.out.println(Thread.currentThread().getName() + " Interrupted!");
                }
            }
        }
        /*
         *
         * 【强制】线程池不允许使用 Executors 去创建，而是通过 ThreadPoolExecutor 的方式，这样的处理方式让写的同学更加明确线程池的运行规则，规避资源耗尽的风险。
         * 说明：Executors 返回的线程池对象的弊端如下：
         *      1）FixedThreadPool 和 SingleThreadPool: 允许的请求队列长度为 Integer.MAX_VALUE，可能会堆积大量的请求，从而导致 OOM。
         *      2）CachedThreadPool 和 ScheduledThreadPool: 允许的创建线程数量为 Integer.MAX_VALUE，可能会创建大量的线程，从而导致 OOM。
         */
        ExecutorService executorService = Executors.newFixedThreadPool(3);
        ExecutorService executorService2 = Executors.newSingleThreadExecutor();
        ExecutorService executorService3 = Executors.newCachedThreadPool();
        ExecutorService executorService4 = Executors.newScheduledThreadPool(3);
        ExecutorService executorService5 = Executors.newWorkStealingPool(3);



        executorService.submit(new MyThread());
        executorService.submit(new MyThread());
        executorService.submit(new MyThread());
        executorService.submit(new MyThread());

        Thread.sleep(2000);

        executorService.shutdown();
        executorService.submit(new MyThread());
        executorService.submit(new MyThread());
//        List<Runnable> list = executorService.shutdownNow();
//        System.out.println(list.size());
    }
}
